package com.hspedu.hspliving.commodity.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hspedu.hspliving.commodity.entity.AttrAttrgroupRelationEntity;
import com.hspedu.hspliving.commodity.service.AttrAttrgroupRelationService;
import com.hspedu.hspliving.commodity.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hspedu.hspliving.commodity.entity.AttrEntity;
import com.hspedu.hspliving.commodity.service.AttrService;
import com.hspedu.common.utils.PageUtils;
import com.hspedu.common.utils.R;



/**
 * 商品属性表
 *
 * @author hsp
 * @email hsp@gmail.com
 * @date 2025-07-27 17:00:00
 */
@RestController
@RequestMapping("commodity/attr")
//没写事务删除，修改表
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("commodity:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);
//        PageUtils page = attrService.queryPageCondition(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/base/list/{categoryId}")
//    @RequiresPermissions("commodity:attr:list")
    public R baseList(@RequestParam Map<String, Object> params,@PathVariable Long categoryId){
        PageUtils page = attrService.queryBasePage(params, categoryId);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/sale/list/{categoryId}")
//    @RequiresPermissions("commodity:attr:list")
    public R saleList(@RequestParam Map<String, Object> params,@PathVariable Long categoryId){
        PageUtils page = attrService.querySalePage(params, categoryId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
//    @RequiresPermissions("commodity:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrEntity attr = attrService.getById(attrId);

        Long categoryId = attr.getCategoryId();
        attr.setCascadedCategoryId(categoryService.getCascadedCategoryId(categoryId));

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("commodity:attr:save")
    public R save(@RequestBody AttrEntity attr){
		attrService.saveAttrAndRelation(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @Transactional
    @RequestMapping("/update")
//    @RequiresPermissions("commodity:attr:update")
    public R update(@RequestBody AttrEntity attr){
		attrService.updateById(attr);

        if(attr.getAttrType()==1){
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            relationEntity.setAttrId(attr.getAttrId());
            relationEntity.setAttrGroupId(attr.getAttrGroupId());
            attrAttrgroupRelationService.update(relationEntity,
                    new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attr.getAttrId()));
        }

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("commodity:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/base/delete")
//    @RequiresPermissions("commodity:attr:delete")
    public R baseDelete(@RequestBody Long[] attrIds){
//		attrService.removeByIds(Arrays.asList(attrIds));
        attrService.removeAndRelation(attrIds);
        return R.ok();
    }

}
